import store from '@/store/index';
import { getItem, setItem } from '@/plugins';

    if (getItem('lang')) {
        store.state.lang = getItem('lang');
    } else {
        const langs = ['tw', 'zh-CN', 'en', 'es', 'pt', 'ja', 'ko', 'hi', 'de', 'ar', 'ru', 'fr', 'vi']
        let browserLang = navigator.language || navigator.languages[0];
        let has = false;
        let item = {};
        langs.forEach(lang => {
            if (browserLang.indexOf(lang) > -1 || browserLang.indexOf(lang.toUpperCase()) > -1){
                has = true;
                item = {
                    value: lang,
                    langText: lang,
                    icon: require('@/assets/image/lang/' + lang + '.png')
                }
            }
        });
        if (has) {
            store.state.lang = item;
            setItem('lang', item);
        } else {

            store.state.lang = {
                id: 2,
                name: 'English',
                value: 'en',
                langText: 'en',
                icon: require('@/assets/image/lang/en.png')
            };

            setItem('lang', {
                id: 2,
                name: 'English',
                value: 'en',
                langText: 'en',
                icon: require('@/assets/image/lang/en.png')
            },
            );
        }
    }
